package com.bwie.controller;


import com.bwie.bean.House;
import com.bwie.bean.Types;
import com.bwie.service.HouseService;
import com.bwie.util.FileUploadUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class HouseController {

    @Resource
    private HouseService houseService;

    @RequestMapping("list.do")
    public String list(
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            String hname,
            String statu,
            Model model){

        Map map = new HashMap();
        map.put("hname",hname);
        map.put("statu",statu);

        PageHelper.startPage(pageNum,3);
        List list= houseService.list(map);
        PageInfo pageInfo = new PageInfo(list);

        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("map",map);
       return "list";
    }


    @RequestMapping("del.do")
    @ResponseBody
    public Object del(Integer hid){
       int i= houseService.del(hid);
       return i;
    }

    @RequestMapping("updacz.do")
    @ResponseBody
//    @Transactional(readOnly = false)
    public Object updacz(Integer hid){
        int i=houseService.updacz(hid);
        return i;
    }

    @RequestMapping("updatz.do")
    @ResponseBody
    public Object updatz(Integer hid){
       int i= houseService.updatz(hid);
       return i;
    }

    @RequestMapping("addto.do")
    public String addto(){
        return "add";
    }


    @RequestMapping("listt.do")
    @ResponseBody
    public Object listt(){
        List<Types> types=houseService.listt();
        return types;
    }


    @RequestMapping("add.do")
    @ResponseBody
    public Object add(House house, HttpSession session,
                      @RequestParam("photo") MultipartFile photo){

        if (photo.getSize()>0){
            String upload = FileUploadUtil.load(session, photo, "upload");
            house.setImgs(upload);
        }
        int i=houseService.add(house);
        return i;
    }

    @RequestMapping("updato.do")
    public String updato(){
        return "upda";
    }

    @RequestMapping("upda.do")
    @ResponseBody
    public Object upda(House house, HttpSession session,
                      @RequestParam("photo") MultipartFile photo){

        if (photo.getSize()>0){
            String upload = FileUploadUtil.load(session, photo, "upload");
            house.setImgs(upload);
        }
        int i=houseService.upda(house);
        return i;
    }

    @RequestMapping("hui.do")
    @ResponseBody
    public Object hui(Integer hid){
       House house= houseService.hui(hid);
       return house;
    }

}
























